using System;
using System.Xml.Serialization;
using Microsoft.Office.Server.Search.Administration;

namespace Carlsberg.SSPC.ContentSources.BO
{
    //Why aren't SharePoints schedules serializeable???
    [XmlRoot("Schedule")]
    public class ScheduleInfo
    {
        //paste
        private int beginYear;
        private int beginMonth;
        private int beginDay;
        private int repeatDuration;
        private int repeatInterval;
        private int startHour;
        private int startMinute;
        private string description;

        private string scheduleType;
        private int daysInterval;
        private int daysOfWeek;
        private int weeksInterval;
        private int daysOfMonth;
        private int monthsOfYear;
        private int weeksOfMonth;

        [XmlAttribute]
        public int BeginYear
        {
            get { return beginYear; }
            set { beginYear = value; }
        }

        [XmlAttribute]
        public int BeginMonth
        {
            get { return beginMonth; }
            set { beginMonth = value; }
        }

        [XmlAttribute]
        public int BeginDay
        {
            get { return beginDay; }
            set { beginDay = value; }
        }

        [XmlAttribute]
        public int RepeatDuration
        {
            get { return repeatDuration; }
            set { repeatDuration = value; }
        }

        [XmlAttribute]
        public int RepeatInterval
        {
            get { return repeatInterval; }
            set { repeatInterval = value; }
        }

        [XmlAttribute]
        public int StartHour
        {
            get { return startHour; }
            set { startHour = value; }
        }

        [XmlAttribute]
        public int StartMinute
        {
            get { return startMinute; }
            set { startMinute = value; }
        }

        [XmlAttribute]
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        [XmlAttribute]
        public string ScheduleType
        {
            get { return scheduleType; }
            set { scheduleType = value; }
        }

        [XmlAttribute]
        public int DaysInterval
        {
            get { return daysInterval; }
            set { daysInterval = value; }
        }

        [XmlAttribute]
        public int DaysOfWeek
        {
            get { return daysOfWeek; }
            set { daysOfWeek = value; }
        }

        [XmlAttribute]
        public int WeeksInterval
        {
            get { return weeksInterval; }
            set { weeksInterval = value; }
        }

        [XmlAttribute]
        public int DaysOfMonth
        {
            get { return daysOfMonth; }
            set { daysOfMonth = value; }
        }

        [XmlAttribute]
        public int MonthsOfYear
        {
            get { return monthsOfYear; }
            set { monthsOfYear = value; }
        }

        [XmlAttribute]
        public int WeeksOfMonth
        {
            get { return weeksOfMonth; }
            set { weeksOfMonth = value; }
        }

        public ScheduleInfo()
        {
        }

        public ScheduleInfo(Schedule schedule)
        {

            //Actually a read only field, so just keep it there for human
            // readability
            Description = schedule.Description;

            BeginDay = schedule.BeginDay;
            BeginMonth = schedule.BeginMonth;
            BeginYear = schedule.BeginYear;
            RepeatDuration = schedule.RepeatDuration;
            RepeatInterval = schedule.RepeatInterval;
            StartHour = schedule.StartHour;
            StartMinute = schedule.StartMinute;

            scheduleType = schedule.GetType().Name;

            if( schedule is DailySchedule )
            {
                daysInterval = ((DailySchedule)schedule).DaysInterval;
            }
            else if( schedule is WeeklySchedule)
            {
                daysOfWeek = (int)((WeeklySchedule)schedule).DaysOfWeek;
                weeksInterval = ((WeeklySchedule) schedule).WeeksInterval;
            }
            else if( schedule is MonthlyDateSchedule)
            {
                daysOfMonth = (int)((MonthlyDateSchedule)schedule).DaysOfMonth;
                monthsOfYear = (int)((MonthlyDateSchedule)schedule).MonthsOfYear;
            }
            else if( schedule is MonthlyDayOfWeekSchedule)
            {
                daysOfWeek = (int)((MonthlyDayOfWeekSchedule) schedule).DaysOfWeek;
                monthsOfYear = (int)((MonthlyDayOfWeekSchedule)schedule).MonthsOfYear;
                weeksOfMonth = (int)((MonthlyDayOfWeekSchedule) schedule).WeeksOfMonth;
            }
            else
            {
                throw new ArgumentException("Unknown type of schedule, found: " + schedule.GetType(),
                                            "schedule");
            }
        }





        public Schedule GetSharePointSchedule(SearchContext searchContext )
        {
            Schedule schedule;

            //Create schedule object and set type specific properties
            switch (scheduleType)
            {
                case "DailySchedule":

                    schedule = new DailySchedule(searchContext);
                    ((DailySchedule) schedule).DaysInterval = daysInterval;
                    break;

                case "WeeklySchedule":
                    schedule = new WeeklySchedule(searchContext);
                    ((WeeklySchedule) schedule).DaysOfWeek = (DaysOfWeek) daysOfWeek;
                    ((WeeklySchedule) schedule).WeeksInterval = weeksInterval;
                    break;

                case "MonthlyDateSchedule":
                    schedule = new MonthlyDateSchedule(searchContext);
                    ((MonthlyDateSchedule) schedule).DaysOfMonth = (DaysOfMonth) daysOfMonth;
                    ((MonthlyDateSchedule) schedule).MonthsOfYear = (MonthsOfYear) monthsOfYear;
                    break;

                case "MonthlyDayOfWeekSchedule":
                    schedule = new MonthlyDayOfWeekSchedule(searchContext);
                    ((MonthlyDayOfWeekSchedule) schedule).DaysOfWeek = (DaysOfWeek) daysOfWeek;
                    ((MonthlyDayOfWeekSchedule) schedule).MonthsOfYear = (MonthsOfYear) monthsOfYear;
                    ((MonthlyDayOfWeekSchedule) schedule).WeeksOfMonth = (WeeksOfMonth) weeksOfMonth;
                    break;

                default:
                    throw new ArgumentException("Unknown type of schedule, found: " 
                        + scheduleType);
            }

            schedule.BeginDay = BeginDay;
            schedule.BeginMonth = BeginMonth;
            schedule.BeginYear = BeginYear;
            schedule.RepeatDuration = RepeatDuration;
            schedule.RepeatInterval = RepeatInterval;
            schedule.StartHour = StartHour;
            schedule.StartMinute = StartMinute;

            return schedule;
        }
    }
}
